# Copyright (c) 2024, json_wang and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document


class CP_Opportunity(Document):
	@frappe.whitelist()
	def make_contract(self):
		if frappe.db.exists({"doctype": "CP_Contract", "opportunity": self.name}):
			raise Exception("该商机已创建过合同，不能重复创建!")
		contract = frappe.new_doc('CP_Contract')
		contract.contract_name = self.name
		contract.opportunity = self.name
		contract.customer = self.party		
		contract.sales_manager = self.sales_manager
		contract.salesman = self.salesman
		contract.contract_total = self.opportunity_total
		contract.insert()
		return contract